package ctrl;

/**
 *
 * @author Anton Karlsson
 */
import entities.GuestUser;
import hotel.jpa.Database;
import hotel.jpa.IJpaCtrl;
import hotel.jpa.UserJpaCtrl;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.ViewUsersBackningBean;

@ManagedBean(name = "userBean")
@RequestScoped
public class UserBean {

    public String saveUser() {
        
        //Using other bean (addRoomBackingBean)
        FacesContext ctx = FacesContext.getCurrentInstance();
        Application app = ctx.getApplication();
        ELContext ectx = ctx.getELContext();
        ExpressionFactory ef = app.getExpressionFactory();
        ValueExpression ve = ef.createValueExpression(ectx, "#{viewUsersBackningBean}", ViewUsersBackningBean.class);
        ViewUsersBackningBean db = (ViewUsersBackningBean) ve.getValue(ectx);
        
        //Create new user and persist it. 
        GuestUser newUser = new GuestUser(db.getName(), db.getEmail(), db.getPwd(), 0);
        
        Database.getUserController().create(newUser);

        return "welcome.xhtml";
    }
}
